from django.shortcuts import render,redirect
#导包
from django.http import HttpResponse,HttpResponseRedirect,JsonResponse
from django.utils.decorators import method_decorator
from myapp.myser import *
#导入类视图
from django.views import View

from myapp.models import *
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
#导入加密库
import hashlib
#导入图片库
#绘画库
from PIL import ImageDraw
#字体库
from PIL import ImageFont
#图片库
from PIL import Image
#随机库
import random
#文件流
import io

import requests

#导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

#导入原生sql模块
from django.db import connection

import jwt

#导入redis数据库
import redis

#导入时间模块
import time

#导入公共目录变量
from mydjango.settings import BASE_DIR

#导包
from django.db.models import Q,F

#导入dwebsocket的库
from dwebsocket.decorators import accept_websocket
import uuid

from PIL import Image,ImageDraw

from myapp.myser import Userser,GoodsSer

# mongoDB
import pymongo


# 关注商品接口(取关)
class GetFlow2(APIView):
    def get(self, request):

        id = request.GET.get("id", None)
        uid = request.GET.get("uid", None)
        type = request.GET.get("type", 1)

        if int(type) == 1:

            # 关注
            userflow = UserFlow(gid=int(id), uid=int(uid))
            try:
                userflow.save()
                message = "关注成功"
                goods = Goods.objects.filter(id=int(id)).first()
                goods.flow = goods.flow + 1
                goods.save()
            except Exception as e:
                print(str(e))
                message = "不能重复关注"
                pass

        elif int(type) == 0:
            # 取关
            UserFlow.objects.filter(gid=int(id), uid=int(uid)).delete()
            message = "取关成功"
            goods = Goods.objects.filter(id=int(id)).first()
            goods.flow = goods.flow - 1
            goods.save()

        else:
            # 批量取关
            ids = request.GET.get("ids", None)
            # 批量操作
            UserFlow.objects.filter(uid=int(uid)).delete()
            # 强转
            id_list = eval(ids)

            for id in id_list:
                userflow = UserFlow(gid=int(id), uid=int(uid))
                userflow.save()

            message = "批量关注成功"

        return Response({'message': message})

# 对结果集美化方法
def dictfetchall(cursor):

    # 获取游标描述
    desc = cursor.description

    return [

        dict(zip([col[0] for col in desc], row))

        for row in cursor.fetchall()

    ]

#商品关注接口(查询用户关注过的商品列表)
class UidFlow2(View):

	def get(self,request):

		uid = request.GET.get("uid",None)

		#建立游标对象
		cursor = connection.cursor()

		#执行sql语句
		cursor.execute('select a.id,a.name,a.create_time from goods a left join userflow b on a.id = b.gid where b.uid = %s ' % str(uid))

		#获取结果
		#result = cursor.fetchall()
		result = dictfetchall(cursor)

		#返回结果 手动序列化
		return HttpResponse(json.dumps(result,ensure_ascii=False,indent=4,default=str),content_type='application/json')